#include <iostream>
#include <string>

#include <unistd.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <strings.h>

enum
{
    SOCKET_ERROR = 1,
    BIND_ERROR,
    LISTEN_ERROR,
    EPOLL_ERROR
};

class EpollServer
{
    const static int MAX_SZ = 64;
public:
    EpollServer(int16_t port)
    {
        _listenfd = socket(AF_INET, SOCK_STREAM, 0);
        if (_listenfd < 0)
        {
            std::cerr << "socket error!" << std::endl;
            exit(SOCKET_ERROR);
        }

        struct sockaddr_in addr;
        bzero(&addr, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = INADDR_ANY;

        int n = bind(_listenfd, (sockaddr*)&addr, sizeof(addr));
        if (n < 0)
        {
            std::cerr << "bind error!" << std::endl;
            exit(BIND_ERROR);
        }

        n = listen(_listenfd, 16);
        if (n < 0)
        {
            std::cerr << "listen error!" << std::endl;
            exit(LISTEN_ERROR);
        }

        _epollfd  = epoll_create(1);
        if (_epollfd < 0)
        {
            std::cerr << "epoll error!" << std::endl;
            exit(EPOLL_ERROR);
        }

        epoll_event event;
        event.events = EPOLLIN;
        event.data.fd = _listenfd;
        epoll_ctl(_epollfd, EPOLL_CTL_ADD, _listenfd, &event);
    }

    void start()
    {
        while (true)
        {
            int n = epoll_wait(_epollfd, _events, MAX_SZ, 1000);
            switch(n)
            {
                case 0:
                    std::clog << "epoll timeout..." << std::endl;
                    break;
                case -1:
                    std::cerr << "epoll error!" << std::endl;
                    exit(EPOLL_ERROR);
                default:
                    handelEvents(n);
            }
        }
    }

private:

    void handelEvents(int sz)
    {
        for (int i = 0; i < sz; i++)
        {
            if (_events[i].data.fd == _listenfd)
                acceptClient();
            else
                serviceIO(_events[i].data.fd);
        }
    }

    void acceptClient()
    {
        // 接收连接
        struct sockaddr_in peer;
        bzero(&peer, sizeof(peer));
        socklen_t len;

        int clientfd = accept(_listenfd, (sockaddr*)&peer, &len);
        if (clientfd < 0)
        {
            std::clog << "accept error!" << std::endl;
            return;
        }

        struct epoll_event event;
        event.events = EPOLLIN;
        event.data.fd = clientfd;
        epoll_ctl(_epollfd, EPOLL_CTL_ADD, clientfd, &event);

        std::clog << clientfd << "add to epoll" << std::endl;
    }

    void serviceIO(int sockfd)
    {
        // 接收数据
        char buffer[1024];
        int n = recv(sockfd, buffer, sizeof(buffer) - 1, 0);

        // 处理数据
        if (n > 0)
        {
            buffer[n] = '\0';
            std::cout << "message: " << buffer << std::endl;

            std::string ret = "echo: " + (std::string)buffer;
            send(sockfd, ret.c_str(), ret.size(), 0);
        }
        else if (n == 0)
        {
            std::clog << sockfd << " exit..." << std::endl;
            epoll_ctl(_epollfd, EPOLL_CTL_DEL, sockfd, nullptr);
            close(sockfd);
        }
        else
        {
            std::cerr << sockfd << " error..." << std::endl;
            epoll_ctl(_epollfd, EPOLL_CTL_DEL, sockfd, nullptr);
            close(sockfd);
        }
    }

private:
    int _listenfd;
    int _epollfd;
    struct epoll_event _events[MAX_SZ];
};